<?php

class Video_Api_Video
{
	protected $_supported = array(
		'www.youtube.com' => 'youtube',
		'youtube.com' => 'youtube',
		'vimeo.com' => 'vimeo',
		'www.dailymotion.com' => 'dailymotion',
	);

	protected $_plugins = array(
		'youtube' => 1,
		'vimeo' => 1,
		'dailymotion' => 1,
		'embedly' => 1,
	);

	/**
	 * @return plugin/false
	 */
	function getPlugin($type)
	{
		if (!$type or !isset($this -> _plugins[$type]))
		{
			return FALSE;
		}
		return Nuo::getPlugin('video/service/' . $type);
	}

	/**
	 * @param string $link
	 * @return array/NULL
	 */
	function getVideoInfo($link)
	{
		$info = parse_url($link);

		$host = isset($info['host']) ? $info['host'] : null;

		if (!$host)
		{
			return FALSE;
		}

		$host = strtolower($host);
		$type = 'embedly';

		if (isset($this -> _supported[$host]))
		{
			$type = $this -> _supported[$host];
		}

		$plugin = Nuo::getPlugin('video/service/' . $type);

		$info = $plugin -> getVideoInfo($link);

		if ($info)
		{
			$info['source_url'] = $link;
			$info['source_host'] = $host;
			$info['plugin'] = $plugin -> getName();
		}

		return $info;
	}

}
